package nl.moviefan.site.controller;

import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import javax.validation.Valid;
import nl.moviefan.site.model.Customer;
import nl.moviefan.site.model.MoviefanUser;
import nl.moviefan.site.service.CustomerService;
import nl.moviefan.site.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Handler for mapping /customers uri.
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    UserService userService;

    @Autowired
    CustomerService customerService;

    /**
     * Methode haalt alle moviefanusers op.
     * @param pageIndex
     * @param size
     * @return 
     */
    @RequestMapping(value = "/pages", method = RequestMethod.GET)
    @ResponseBody
    public Page<MoviefanUser> getUsers(@RequestParam(value = "pageIndex", required = false, defaultValue = "0") Integer pageIndex, @RequestParam(value = "size", required = false, defaultValue = "20") Integer size) {
        Pageable pageable = new PageRequest(pageIndex, size, new Sort("id"));
        Page<MoviefanUser> pageResult = userService.findAll(pageable);
        return pageResult;
    }

    /**
     * Methode haalt Moviefanuser object op behorend bij username.
     * @param username
     * @return 
     */
    @RequestMapping(value = "/getUserByName", method = RequestMethod.GET)
    @ResponseBody
    public MoviefanUser getUserByName(@RequestParam(value = "name", required = true) String username) {
        MoviefanUser moviefanUser = userService.findByUsername(username);
        return moviefanUser;
    }

    /**
     * Methode vangt POST op van /edit uri met aangepaste gegevens van Customer.
     * @param customer
     * @param principal
     * @param bindingResult
     * @param model
     * @return 
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ModelAndView updateCustomer(@ModelAttribute("customer") @Valid Customer customer, Principal principal, BindingResult bindingResult, ModelAndView model) {
        if (bindingResult.hasErrors()) {
            return model;
        }
        String username = principal.getName();
        MoviefanUser moviefanuser = userService.findByUsername(username);
        customer.setMoviefanUser(moviefanuser);
        customer.setIsActive(true);
        Customer customerSaved = customerService.save(customer);
        return model;
    }

    /**
     * Method to get the details for the current user
     *
     * @param principal get current user with principal object
     * @return ModelAndView of details page of current user
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public ModelAndView getCustomerDetails(Principal principal) {
        String currentUserName = principal.getName();
        Customer customer = customerService.getCustomerByUsername(currentUserName);
        ModelAndView modelandview = new ModelAndView();
        modelandview.addObject("customer", customer);
        return modelandview;
    }

    /**
     * Methode handelt POST request af van /create uri met aangemaakte 
     * customer object.
     * @param customer
     * @param bindingResult
     * @param model
     * @return 
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ModelAndView createCustomer(@ModelAttribute("customer") Customer customer, BindingResult bindingResult, ModelAndView model) {
        if (bindingResult.hasErrors()) {
            return model;
        }
        customer.getMoviefanUser().setId(-1);
        Customer customerToBeSaved = customerService.insertCustomer(customer);
        Customer customerSaved = customerService.save(customerToBeSaved);
        return new ModelAndView("login");
    }

    /**
     * Methode mapt /create uri, maakt nieuw Customer object aan en voegt alle
     * bekende vestigingid's toe aan ModelAndView object.
     * @param principal
     * @return 
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView getCreateCustomerForm(Principal principal) {
        List<Integer> alleVestigingenIDs = this.getAlleVestigingenIDs();
        int nieuwKlantnummer = customerService.getNieuwKlantnummer();

        ModelAndView modelandview = new ModelAndView();
        modelandview.addObject("customer", new Customer());
        modelandview.addObject("vestigingen", alleVestigingenIDs);
        modelandview.addObject("nieuwKlantnummer", nieuwKlantnummer);
        return modelandview;
    }

    /**
     * Methode genereert alle vestigingids.
     * @return 
     */
    private List<Integer> getAlleVestigingenIDs() {
        List<Integer> list = new ArrayList();
        list.add(1);
        return list;
    }
}
